<template>
  <div>
    <el-card>
      <div slot="header">
        <span>修改密码</span>
      </div>
      <div class="content">
        <el-form
          :model="passwordForm"
          :rules="rules"
          ref="passwordForm"
          label-width="100px"
        >
          <el-form-item label="旧密码" prop="oldPwd">
            <el-input
              type="password"
              v-model="passwordForm.oldPwd"
              autocomplete="off"
              show-password
              placeholder="请输入旧密码"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPwd1">
            <el-input
              type="password"
              v-model="passwordForm.newPwd1"
              autocomplete="off"
              show-password
              placeholder="请输入新密码"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="newPwd2">
            <el-input
              type="password"
              v-model="passwordForm.newPwd2"
              autocomplete="off"
              show-password
              placeholder="请输入确认密码"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">确定</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import {usersEditpwd,checkOldpwd} from '@/api/user'
export default {
  data() {
    //旧密码验证
     const validatorOldPwd =async (rules, value, callback) => {
      // rule   验证规则
      // value  输入的值
      // callback  回调函数
      if (!value) {
        return callback(new Error("请输入旧密码!"));
      } else if (!/^[a-zA-Z0-9_-]{6,12}$/.test(value)) {
        callback(new Error("密码6-12位 数字 字母 下划线 减号"));
      } else {
        //接口位置
        //找后端接口  验证密码  发送ajax
        let res = await checkOldpwd({oldPwd:value})
        // console.log(res);//响应参数
        let {code ,msg}=res;
       if (code === 200) {
            this.$message({
              message: msg,
              type: "success",
            });
          } else {
           this.$message.error(msg);
           callback(new Error("原密码错误!"));
        }
      }
    };
    //新密码验证
    const validatorNewPwd1 = (rules, value, callback) => {
  
        if (!value) {
        return callback(new Error("请输入新密码!"));
      } else if (!/^[a-zA-Z0-9_-]{6,12}$/.test(value)) {
        callback(new Error("密码6-12位 数字 字母 下划线 减号"));
      } else if(value===this.passwordForm.oldPwd){//新密码不能和旧密码一样
          callback(new Error("新密码不能和旧密码一样"));
      }else{
        //验证确认密码
        this.$refs.passwordForm.validateField("newPwd2")
        callback();//成功
      }
    };
    //确认密码验证
    const validatorNewPwd2 =(rules, value, callback) => {
    
      if (!value) {
         return callback(new Error("请输入确认密码!"));
      } else if(!(value===this.passwordForm.newPwd1)){//两次密码不一致
          callback(new Error("两次密码不一致!"));
      }else if (!/^[a-zA-Z0-9_-]{6,12}$/.test(value)) {
        callback(new Error("密码6-12位 数字 字母 下划线 减号"));
      }else{
        callback();//成功
      }
    };
    return {
      //表单数据
      passwordForm: {
        oldPwd: "", //旧密码
        newPwd1: "", //新密码
        newPwd2: "", //确认密码
      },
      // 验证规则
      rules: {
        oldPwd: [
          { validator: validatorOldPwd, trigger: "blur" }, //validatOroldPwd放在 data  return 之前
        ],
        newPwd1: [
          { validator: validatorNewPwd1, trigger: "blur" }, //validatorNewPwd1放在 data  return 之前
        ],
        newPwd2: [
          { validator: validatorNewPwd2, trigger: "blur" }, //validatorNewPwd2放在 data  return 之前
        ],
      },
    };
  },
  methods: {
    //修改密码
    submitForm() {
      this.$refs.passwordForm.validate(async valis=>{
      if(valis){
          // console.log("1");//进入事件
          let res = await usersEditpwd({newPwd:this.passwordForm.newPwd2});
          // console.log(res);//修改密码响应信息
           let { code, msg } = res;
          // console.log(code,msg);//code:0 成功  msg:提示
          if (code === 0) {
            this.$message({
              message: msg,
              type: "success",
            });
            //重置表单
            this.$refs.passwordForm.resetFields();
          } else {
           this.$message.error(msg);
        }
      } 
      else {
        this.$message.error("请补充完整");
      }
    })},
    //重置表单
    resetForm(){
       this.$refs.passwordForm.resetFields();
    }
     

  },
};
</script>

<style lang="less" scoped>
.content {
  width: 400px;
}
</style>